//
//  ViewController.m
//  PODRenderer
//
//  Created by ajit on 6/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

@synthesize glViewContainer;

@synthesize btnUp, btnDown, btnLeft, btnRight, btnDock, btnIn, btnOut;

@synthesize lblLX, lblLY, lblLZ, lblRX, lblRY, lblRZ, lblCurrentAccuracy;

@synthesize controlPanel;

@synthesize sgmtMoveRotate, sliderAccurary;

@synthesize glController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return NO;
}


#pragma mark - View Controller IBAction Methods

- (IBAction) btnDockPressed:(UIButton*) sender {
    [self dockViewInOut];
}


- (IBAction) btnMovemenPressed:(UIButton*) sender {
    
    if(glController != nil) {
        
        if([sgmtMoveRotate selectedSegmentIndex] == 0) {
    
            if([sender.titleLabel.text isEqualToString:@"Up"]) {
                //id = 1
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :1 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Down"]) {
                //id = 2
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :2 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Left"]) {
                //id = 3
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :3 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Right"]) {
                //id = 4
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :4 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"In"]) {
                //id = 5
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :5 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Out"]) {
                //id = 6
                [glController moveTarget :([sgmtTarget selectedSegmentIndex]+1) :6 :[sliderAccurary value]];
            }
            [glController updateLocation :([sgmtTarget selectedSegmentIndex]+1) :lblLX :lblLY :lblLZ];
        } else {
            if([sender.titleLabel.text isEqualToString:@"Up"]) {
                //id = 1
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :1 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Down"]) {
                //id = 2
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :2 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Left"]) {
                //id = 3
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :3 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Right"]) {
                //id = 4
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :4 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"In"]) {
                //id = 5
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :5 :[sliderAccurary value]];
            } else if([sender.titleLabel.text isEqualToString:@"Out"]) {
                //id = 6
                [glController rotateTarget :([sgmtTarget selectedSegmentIndex]+1) :6 :[sliderAccurary value]];
            }
            [glController updateRotation :([sgmtTarget selectedSegmentIndex]+1) :lblRX :lblRY :lblRZ];
        }
    }
    
}

- (IBAction)sliderAccuracyValueChanged:(UISlider*)sender {
    [lblCurrentAccuracy setText:[NSString stringWithFormat:@"Current Accuracy : %2.2f", [sender value]]];
}


#pragma mark - View Controller Specific Methods

- (void) dockViewInOut {
    
    CGRect newFrame = self.controlPanel.frame;
    
    if(self.controlPanel.frame.origin.x == 468.0) {
        newFrame.origin.x = 728.0;
        self.controlPanel.frame = newFrame;
        [self.btnDock setTitle:@"<" forState:UIControlStateNormal];
    } else {
        newFrame.origin.x = 468.0;
        self.controlPanel.frame = newFrame;
        [self.btnDock setTitle:@">" forState:UIControlStateNormal];
    }
    
}

- (IBAction) sgmtTarget:(UISegmentedControl*)sender {
    [glController updateLocation :([sgmtTarget selectedSegmentIndex]+1) :lblLX :lblLY :lblLZ];
    [glController updateRotation :([sgmtTarget selectedSegmentIndex]+1) :lblRX :lblRY :lblRZ];
    
}
- (IBAction) sgmtMoveRotateValueChanged:(UISegmentedControl*)sender {
    [glController updateLocation :([sgmtTarget selectedSegmentIndex]+1) :lblLX :lblLY :lblLZ];
    [glController updateRotation :([sgmtTarget selectedSegmentIndex]+1) :lblRX :lblRY :lblRZ];
}

@end
